using UnityEngine;

public class CameraAspectRatioKeeper : MonoBehaviour
{
	public static CameraAspectRatioKeeper Instance;

	public Vector2 aspectRatio = new Vector2(16f, 9f);

	private int ScreenSizeX;

	private int ScreenSizeY;

	private Camera _camera;

	private void OnPreCull()
	{
		if (!Application.isEditor)
		{
			Rect rect = Camera.main.rect;
			Rect rect2 = new Rect(0f, 0f, 1f, 1f);
			Camera.main.rect = rect2;
			Camera.main.rect = rect;
		}
	}

	private void Start()
	{
		if (!Instance)
		{
			Instance = this;
			Object.DontDestroyOnLoad(this);
		}
		else
		{
			Object.Destroy(this);
		}
	}

	private void Update()
	{
		if (!(_camera == Camera.main) || Screen.width != ScreenSizeX || Screen.height != ScreenSizeY)
		{
			if (_camera != Camera.main)
			{
				_camera = Camera.main;
			}
			float num = aspectRatio.x / aspectRatio.y;
			float num2 = (float)Screen.width / (float)Screen.height / num;
			if (num2 < 1f)
			{
				Rect rect = Camera.main.rect;
				rect.width = 1f;
				rect.height = num2;
				rect.x = 0f;
				rect.y = (1f - num2) / 2f;
				Camera.main.rect = rect;
			}
			else
			{
				float num3 = 1f / num2;
				Rect rect2 = Camera.main.rect;
				rect2.width = num3;
				rect2.height = 1f;
				rect2.x = (1f - num3) / 2f;
				rect2.y = 0f;
				Camera.main.rect = rect2;
			}
			ScreenSizeX = Screen.width;
			ScreenSizeY = Screen.height;
		}
	}
}
